<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>object_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_object_object_test.js.html">object_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.objectTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.objectTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.functions&#39;);
<a name="line19"></a>goog.require(&#39;goog.object&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>
<a name="line22"></a>function stringifyObject(m) {
<a name="line23"></a>  var keys = goog.object.getKeys(m);
<a name="line24"></a>  var s = &#39;&#39;;
<a name="line25"></a>  for (var i = 0; i &lt; keys.length; i++) {
<a name="line26"></a>    s += keys[i] + goog.object.get(m, keys[i]);
<a name="line27"></a>  }
<a name="line28"></a>  return s;
<a name="line29"></a>}
<a name="line30"></a>
<a name="line31"></a>function getObject() {
<a name="line32"></a>  return {
<a name="line33"></a>    a: 0,
<a name="line34"></a>    b: 1,
<a name="line35"></a>    c: 2,
<a name="line36"></a>    d: 3
<a name="line37"></a>  };
<a name="line38"></a>}
<a name="line39"></a>
<a name="line40"></a>function testKeys() {
<a name="line41"></a>  var m = getObject();
<a name="line42"></a>  assertEquals(&#39;getKeys, The keys should be a,b,c&#39;,
<a name="line43"></a>               &#39;a,b,c,d&#39;,
<a name="line44"></a>               goog.object.getKeys(m).join(&#39;,&#39;));
<a name="line45"></a>}
<a name="line46"></a>
<a name="line47"></a>function testValues() {
<a name="line48"></a>  var m = getObject();
<a name="line49"></a>  assertEquals(&#39;getValues, The values should be 0,1,2&#39;,
<a name="line50"></a>      &#39;0,1,2,3&#39;, goog.object.getValues(m).join(&#39;,&#39;));
<a name="line51"></a>}
<a name="line52"></a>
<a name="line53"></a>function testGetAnyKey() {
<a name="line54"></a>  var m = getObject();
<a name="line55"></a>  assertTrue(&#39;getAnyKey, The key should be a,b,c or d&#39;,
<a name="line56"></a>             goog.object.getAnyKey(m) in m);
<a name="line57"></a>  assertUndefined(&#39;getAnyKey, The key should be undefined&#39;,
<a name="line58"></a>                  goog.object.getAnyKey({}));
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>function testGetAnyValue() {
<a name="line62"></a>  var m = getObject();
<a name="line63"></a>  assertTrue(&#39;getAnyValue, The value should be 0,1,2 or 3&#39;,
<a name="line64"></a>             goog.object.containsValue(m, goog.object.getAnyValue(m)));
<a name="line65"></a>  assertUndefined(&#39;getAnyValue, The value should be undefined&#39;,
<a name="line66"></a>                  goog.object.getAnyValue({}));
<a name="line67"></a>}
<a name="line68"></a>
<a name="line69"></a>function testContainsKey() {
<a name="line70"></a>  var m = getObject();
<a name="line71"></a>  assertTrue(&quot;containsKey, Should contain the &#39;a&#39; key&quot;,
<a name="line72"></a>             goog.object.containsKey(m, &#39;a&#39;));
<a name="line73"></a>  assertFalse(&quot;containsKey, Should not contain the &#39;e&#39; key&quot;,
<a name="line74"></a>              goog.object.containsKey(m, &#39;e&#39;));
<a name="line75"></a>}
<a name="line76"></a>
<a name="line77"></a>function testContainsValue() {
<a name="line78"></a>  var m = getObject();
<a name="line79"></a>  assertTrue(&#39;containsValue, Should contain the value 0&#39;,
<a name="line80"></a>             goog.object.containsValue(m, 0));
<a name="line81"></a>  assertFalse(&#39;containsValue, Should not contain the value 4&#39;,
<a name="line82"></a>              goog.object.containsValue(m, 4));
<a name="line83"></a>  assertTrue(&#39;isEmpty, The map should not be empty&#39;, !goog.object.isEmpty(m));
<a name="line84"></a>}
<a name="line85"></a>
<a name="line86"></a>function testFindKey() {
<a name="line87"></a>  var dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4};
<a name="line88"></a>  var key = goog.object.findKey(dict, function(v, k, d) {
<a name="line89"></a>    assertEquals(&#39;valid 3rd argument&#39;, dict, d);
<a name="line90"></a>    assertTrue(&#39;valid 1st argument&#39;, goog.object.containsValue(d, v));
<a name="line91"></a>    assertTrue(&#39;valid 2nd argument&#39;, k in d);
<a name="line92"></a>    return v % 3 == 0;
<a name="line93"></a>  });
<a name="line94"></a>  assertEquals(&#39;key &quot;c&quot; found&#39;, &#39;c&#39;, key);
<a name="line95"></a>
<a name="line96"></a>  var pred = function(value) {
<a name="line97"></a>    return value &gt; 5;
<a name="line98"></a>  };
<a name="line99"></a>  assertUndefined(&#39;no match&#39;, goog.object.findKey(dict, pred));
<a name="line100"></a>}
<a name="line101"></a>
<a name="line102"></a>function testFindValue() {
<a name="line103"></a>  var dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4};
<a name="line104"></a>  var value = goog.object.findValue(dict, function(v, k, d) {
<a name="line105"></a>    assertEquals(&#39;valid 3rd argument&#39;, dict, d);
<a name="line106"></a>    assertTrue(&#39;valid 1st argument&#39;, goog.object.containsValue(d, v));
<a name="line107"></a>    assertTrue(&#39;valid 2nd argument&#39;, k in d);
<a name="line108"></a>    return k.toUpperCase() == &#39;C&#39;;
<a name="line109"></a>  });
<a name="line110"></a>  assertEquals(&#39;value 3 found&#39;, 3, value);
<a name="line111"></a>
<a name="line112"></a>  var pred = function(value, key) {
<a name="line113"></a>    return key &gt; &#39;d&#39;;
<a name="line114"></a>  };
<a name="line115"></a>  assertUndefined(&#39;no match&#39;, goog.object.findValue(dict, pred));
<a name="line116"></a>}
<a name="line117"></a>
<a name="line118"></a>function testClear() {
<a name="line119"></a>  var m = getObject();
<a name="line120"></a>  goog.object.clear(m);
<a name="line121"></a>  assertTrue(&#39;cleared so it should be empty&#39;, goog.object.isEmpty(m));
<a name="line122"></a>  assertFalse(&quot;cleared so it should not contain &#39;a&#39; key&quot;,
<a name="line123"></a>              goog.object.containsKey(m, &#39;a&#39;));
<a name="line124"></a>}
<a name="line125"></a>
<a name="line126"></a>function testClone() {
<a name="line127"></a>  var m = getObject();
<a name="line128"></a>  var m2 = goog.object.clone(m);
<a name="line129"></a>  assertFalse(&#39;clone so it should not be empty&#39;, goog.object.isEmpty(m2));
<a name="line130"></a>  assertTrue(&quot;clone so it should contain &#39;c&#39; key&quot;,
<a name="line131"></a>             goog.object.containsKey(m2, &#39;c&#39;));
<a name="line132"></a>}
<a name="line133"></a>
<a name="line134"></a>function testUnsafeClonePrimitive() {
<a name="line135"></a>  assertEquals(&#39;cloning a primitive should return an equal primitive&#39;,
<a name="line136"></a>      5, goog.object.unsafeClone(5));
<a name="line137"></a>}
<a name="line138"></a>
<a name="line139"></a>function testUnsafeCloneObjectThatHasACloneMethod() {
<a name="line140"></a>  var original = {
<a name="line141"></a>    name: &#39;original&#39;,
<a name="line142"></a>    clone: goog.functions.constant({name: &#39;clone&#39;})
<a name="line143"></a>  };
<a name="line144"></a>
<a name="line145"></a>  var clone = goog.object.unsafeClone(original);
<a name="line146"></a>  assertEquals(&#39;original&#39;, original.name);
<a name="line147"></a>  assertEquals(&#39;clone&#39;, clone.name);
<a name="line148"></a>}
<a name="line149"></a>
<a name="line150"></a>function testUnsafeCloneFlatObject() {
<a name="line151"></a>  var original = {a: 1, b: 2, c: 3};
<a name="line152"></a>  var clone = goog.object.unsafeClone(original);
<a name="line153"></a>  assertNotEquals(original, clone);
<a name="line154"></a>  assertObjectEquals(original, clone);
<a name="line155"></a>}
<a name="line156"></a>
<a name="line157"></a>function testUnsafeCloneDeepObject() {
<a name="line158"></a>  var original = {
<a name="line159"></a>    a: 1,
<a name="line160"></a>    b: {c: 2, d: 3},
<a name="line161"></a>    e: {f: {g: 4, h: 5}}
<a name="line162"></a>  };
<a name="line163"></a>  var clone = goog.object.unsafeClone(original);
<a name="line164"></a>
<a name="line165"></a>  assertNotEquals(original, clone);
<a name="line166"></a>  assertNotEquals(original.b, clone.b);
<a name="line167"></a>  assertNotEquals(original.e, clone.e);
<a name="line168"></a>
<a name="line169"></a>  assertEquals(1, clone.a);
<a name="line170"></a>  assertEquals(2, clone.b.c);
<a name="line171"></a>  assertEquals(3, clone.b.d);
<a name="line172"></a>  assertEquals(4, clone.e.f.g);
<a name="line173"></a>  assertEquals(5, clone.e.f.h);
<a name="line174"></a>}
<a name="line175"></a>
<a name="line176"></a>function testUnsafeCloneFunctions() {
<a name="line177"></a>  var original = {
<a name="line178"></a>    f: goog.functions.constant(&#39;hi&#39;)
<a name="line179"></a>  };
<a name="line180"></a>  var clone = goog.object.unsafeClone(original);
<a name="line181"></a>
<a name="line182"></a>  assertNotEquals(original, clone);
<a name="line183"></a>  assertEquals(&#39;hi&#39;, clone.f());
<a name="line184"></a>  assertEquals(original.f, clone.f);
<a name="line185"></a>}
<a name="line186"></a>
<a name="line187"></a>function testForEach() {
<a name="line188"></a>  var m = getObject();
<a name="line189"></a>  var s = &#39;&#39;;
<a name="line190"></a>  goog.object.forEach(m, function(val, key, m2) {
<a name="line191"></a>    assertNotUndefined(key);
<a name="line192"></a>    assertEquals(m, m2);
<a name="line193"></a>    s += key + val;
<a name="line194"></a>  });
<a name="line195"></a>  assertEquals(s, &#39;a0b1c2d3&#39;);
<a name="line196"></a>}
<a name="line197"></a>
<a name="line198"></a>function testFilter() {
<a name="line199"></a>  var m = getObject();
<a name="line200"></a>
<a name="line201"></a>  var m2 = goog.object.filter(m, function(val, key, m3) {
<a name="line202"></a>    assertNotUndefined(key);
<a name="line203"></a>    assertEquals(m, m3);
<a name="line204"></a>    return val &gt; 1;
<a name="line205"></a>  });
<a name="line206"></a>  assertEquals(stringifyObject(m2), &#39;c2d3&#39;);
<a name="line207"></a>}
<a name="line208"></a>
<a name="line209"></a>
<a name="line210"></a>function testMap() {
<a name="line211"></a>  var m = getObject();
<a name="line212"></a>  var m2 = goog.object.map(m, function(val, key, m3) {
<a name="line213"></a>    assertNotUndefined(key);
<a name="line214"></a>    assertEquals(m, m3);
<a name="line215"></a>    return val * val;
<a name="line216"></a>  });
<a name="line217"></a>  assertEquals(stringifyObject(m2), &#39;a0b1c4d9&#39;);
<a name="line218"></a>}
<a name="line219"></a>
<a name="line220"></a>function testSome() {
<a name="line221"></a>  var m = getObject();
<a name="line222"></a>  var b = goog.object.some(m, function(val, key, m2) {
<a name="line223"></a>    assertNotUndefined(key);
<a name="line224"></a>    assertEquals(m, m2);
<a name="line225"></a>    return val &gt; 1;
<a name="line226"></a>  });
<a name="line227"></a>  assertTrue(b);
<a name="line228"></a>  var b = goog.object.some(m, function(val, key, m2) {
<a name="line229"></a>    assertNotUndefined(key);
<a name="line230"></a>    assertEquals(m, m2);
<a name="line231"></a>    return val &gt; 100;
<a name="line232"></a>  });
<a name="line233"></a>  assertFalse(b);
<a name="line234"></a>}
<a name="line235"></a>
<a name="line236"></a>function testEvery() {
<a name="line237"></a>  var m = getObject();
<a name="line238"></a>  var b = goog.object.every(m, function(val, key, m2) {
<a name="line239"></a>    assertNotUndefined(key);
<a name="line240"></a>    assertEquals(m, m2);
<a name="line241"></a>    return val &gt;= 0;
<a name="line242"></a>  });
<a name="line243"></a>  assertTrue(b);
<a name="line244"></a>  b = goog.object.every(m, function(val, key, m2) {
<a name="line245"></a>    assertNotUndefined(key);
<a name="line246"></a>    assertEquals(m, m2);
<a name="line247"></a>    return val &gt; 1;
<a name="line248"></a>  });
<a name="line249"></a>  assertFalse(b);
<a name="line250"></a>}
<a name="line251"></a>
<a name="line252"></a>function testContains() {
<a name="line253"></a>  var m = getObject();
<a name="line254"></a>  assertTrue(goog.object.contains(m, 3));
<a name="line255"></a>  assertFalse(goog.object.contains(m, 4));
<a name="line256"></a>}
<a name="line257"></a>
<a name="line258"></a>function testObjectProperties() {
<a name="line259"></a>  var m = {};
<a name="line260"></a>
<a name="line261"></a>  goog.object.set(m, &#39;toString&#39;, &#39;once&#39;);
<a name="line262"></a>  goog.object.set(m, &#39;valueOf&#39;, &#39;upon&#39;);
<a name="line263"></a>  goog.object.set(m, &#39;eval&#39;, &#39;a&#39;);
<a name="line264"></a>  goog.object.set(m, &#39;toSource&#39;, &#39;midnight&#39;);
<a name="line265"></a>  goog.object.set(m, &#39;prototype&#39;, &#39;dreary&#39;);
<a name="line266"></a>  goog.object.set(m, &#39;hasOwnProperty&#39;, &#39;dark&#39;);
<a name="line267"></a>
<a name="line268"></a>  assertEquals(goog.object.get(m, &#39;toString&#39;), &#39;once&#39;);
<a name="line269"></a>  assertEquals(goog.object.get(m, &#39;valueOf&#39;), &#39;upon&#39;);
<a name="line270"></a>  assertEquals(goog.object.get(m, &#39;eval&#39;), &#39;a&#39;);
<a name="line271"></a>  assertEquals(goog.object.get(m, &#39;toSource&#39;), &#39;midnight&#39;);
<a name="line272"></a>  assertEquals(goog.object.get(m, &#39;prototype&#39;), &#39;dreary&#39;);
<a name="line273"></a>  assertEquals(goog.object.get(m, &#39;hasOwnProperty&#39;), &#39;dark&#39;);
<a name="line274"></a>}
<a name="line275"></a>
<a name="line276"></a>function testSetDefault() {
<a name="line277"></a>  var dict = {};
<a name="line278"></a>  assertEquals(1, goog.object.setIfUndefined(dict, &#39;a&#39;, 1));
<a name="line279"></a>  assertEquals(1, dict[&#39;a&#39;]);
<a name="line280"></a>  assertEquals(1, goog.object.setIfUndefined(dict, &#39;a&#39;, 2));
<a name="line281"></a>  assertEquals(1, dict[&#39;a&#39;]);
<a name="line282"></a>}
<a name="line283"></a>
<a name="line284"></a>function testTranspose() {
<a name="line285"></a>  var m = getObject();
<a name="line286"></a>  var b = goog.object.transpose(m);
<a name="line287"></a>  assertEquals(&#39;a&#39;, b[0]);
<a name="line288"></a>  assertEquals(&#39;b&#39;, b[1]);
<a name="line289"></a>  assertEquals(&#39;c&#39;, b[2]);
<a name="line290"></a>  assertEquals(&#39;d&#39;, b[3]);
<a name="line291"></a>}
<a name="line292"></a>
<a name="line293"></a>function testExtend() {
<a name="line294"></a>  var o = {};
<a name="line295"></a>  var o2 = {a: 0, b: 1};
<a name="line296"></a>  goog.object.extend(o, o2);
<a name="line297"></a>  assertEquals(0, o.a);
<a name="line298"></a>  assertEquals(1, o.b);
<a name="line299"></a>  assertTrue(&#39;a&#39; in o);
<a name="line300"></a>  assertTrue(&#39;b&#39; in o);
<a name="line301"></a>
<a name="line302"></a>  o2 = {c: 2};
<a name="line303"></a>  goog.object.extend(o, o2);
<a name="line304"></a>  assertEquals(2, o.c);
<a name="line305"></a>  assertTrue(&#39;c&#39; in o);
<a name="line306"></a>
<a name="line307"></a>  o2 = {c: 3};
<a name="line308"></a>  goog.object.extend(o, o2);
<a name="line309"></a>  assertEquals(3, o.c);
<a name="line310"></a>  assertTrue(&#39;c&#39; in o);
<a name="line311"></a>
<a name="line312"></a>  o = {};
<a name="line313"></a>  o2 = {c: 2};
<a name="line314"></a>  var o3 = {c: 3};
<a name="line315"></a>  goog.object.extend(o, o2, o3);
<a name="line316"></a>  assertEquals(3, o.c);
<a name="line317"></a>  assertTrue(&#39;c&#39; in o);
<a name="line318"></a>
<a name="line319"></a>  o = {};
<a name="line320"></a>  o2 = {a: 0, b: 1};
<a name="line321"></a>  o3 = {c: 2, d: 3};
<a name="line322"></a>  goog.object.extend(o, o2, o3);
<a name="line323"></a>  assertEquals(0, o.a);
<a name="line324"></a>  assertEquals(1, o.b);
<a name="line325"></a>  assertEquals(2, o.c);
<a name="line326"></a>  assertEquals(3, o.d);
<a name="line327"></a>  assertTrue(&#39;a&#39; in o);
<a name="line328"></a>  assertTrue(&#39;b&#39; in o);
<a name="line329"></a>  assertTrue(&#39;c&#39; in o);
<a name="line330"></a>  assertTrue(&#39;d&#39; in o);
<a name="line331"></a>
<a name="line332"></a>  o = {};
<a name="line333"></a>  o2 = {
<a name="line334"></a>    &#39;constructor&#39;: 0,
<a name="line335"></a>    &#39;hasOwnProperty&#39;: 1,
<a name="line336"></a>    &#39;isPrototypeOf&#39;: 2,
<a name="line337"></a>    &#39;propertyIsEnumerable&#39;: 3,
<a name="line338"></a>    &#39;toLocaleString&#39;: 4,
<a name="line339"></a>    &#39;toString&#39;: 5,
<a name="line340"></a>    &#39;valueOf&#39;: 6
<a name="line341"></a>  };
<a name="line342"></a>  goog.object.extend(o, o2);
<a name="line343"></a>  assertEquals(0, o[&#39;constructor&#39;]);
<a name="line344"></a>  assertEquals(1, o[&#39;hasOwnProperty&#39;]);
<a name="line345"></a>  assertEquals(2, o[&#39;isPrototypeOf&#39;]);
<a name="line346"></a>  assertEquals(3, o[&#39;propertyIsEnumerable&#39;]);
<a name="line347"></a>  assertEquals(4, o[&#39;toLocaleString&#39;]);
<a name="line348"></a>  assertEquals(5, o[&#39;toString&#39;]);
<a name="line349"></a>  assertEquals(6, o[&#39;valueOf&#39;]);
<a name="line350"></a>  assertTrue(&#39;constructor&#39; in o);
<a name="line351"></a>  assertTrue(&#39;hasOwnProperty&#39; in o);
<a name="line352"></a>  assertTrue(&#39;isPrototypeOf&#39; in o);
<a name="line353"></a>  assertTrue(&#39;propertyIsEnumerable&#39; in o);
<a name="line354"></a>  assertTrue(&#39;toLocaleString&#39; in o);
<a name="line355"></a>  assertTrue(&#39;toString&#39; in o);
<a name="line356"></a>  assertTrue(&#39;valueOf&#39; in o);
<a name="line357"></a>}
<a name="line358"></a>
<a name="line359"></a>function testCreate() {
<a name="line360"></a>  assertObjectEquals(&#39;With multiple arguments&#39;,
<a name="line361"></a>                     {a: 0, b: 1}, goog.object.create(&#39;a&#39;, 0, &#39;b&#39;, 1));
<a name="line362"></a>  assertObjectEquals(&#39;With an array argument&#39;,
<a name="line363"></a>                     {a: 0, b: 1}, goog.object.create([&#39;a&#39;, 0, &#39;b&#39;, 1]));
<a name="line364"></a>
<a name="line365"></a>  assertObjectEquals(&#39;With no arguments&#39;,
<a name="line366"></a>                     {}, goog.object.create());
<a name="line367"></a>  assertObjectEquals(&#39;With an ampty array argument&#39;,
<a name="line368"></a>                     {}, goog.object.create([]));
<a name="line369"></a>
<a name="line370"></a>  assertThrows(&#39;Should throw due to uneven arguments&#39;, function() {
<a name="line371"></a>    goog.object.create(&#39;a&#39;);
<a name="line372"></a>  });
<a name="line373"></a>  assertThrows(&#39;Should throw due to uneven arguments&#39;, function() {
<a name="line374"></a>    goog.object.create(&#39;a&#39;, 0, &#39;b&#39;);
<a name="line375"></a>  });
<a name="line376"></a>  assertThrows(&#39;Should throw due to uneven length array&#39;, function() {
<a name="line377"></a>    goog.object.create([&#39;a&#39;]);
<a name="line378"></a>  });
<a name="line379"></a>  assertThrows(&#39;Should throw due to uneven length array&#39;, function() {
<a name="line380"></a>    goog.object.create([&#39;a&#39;, 0, &#39;b&#39;]);
<a name="line381"></a>  });
<a name="line382"></a>}
<a name="line383"></a>
<a name="line384"></a>function testCreateSet() {
<a name="line385"></a>  assertObjectEquals(&#39;With multiple arguments&#39;,
<a name="line386"></a>                     {a: true, b: true}, goog.object.createSet(&#39;a&#39;, &#39;b&#39;));
<a name="line387"></a>  assertObjectEquals(&#39;With an array argument&#39;,
<a name="line388"></a>                     {a: true, b: true}, goog.object.createSet([&#39;a&#39;, &#39;b&#39;]));
<a name="line389"></a>
<a name="line390"></a>  assertObjectEquals(&#39;With no arguments&#39;,
<a name="line391"></a>                     {}, goog.object.createSet());
<a name="line392"></a>  assertObjectEquals(&#39;With an ampty array argument&#39;,
<a name="line393"></a>                     {}, goog.object.createSet([]));
<a name="line394"></a>}
<a name="line395"></a>
<a name="line396"></a>function createTestDeepObject() {
<a name="line397"></a>  var obj = {};
<a name="line398"></a>  obj.a = {};
<a name="line399"></a>  obj.a.b = {};
<a name="line400"></a>  obj.a.b.c = {};
<a name="line401"></a>  obj.a.b.c.fooArr = [5, 6, 7, 8];
<a name="line402"></a>  obj.a.b.c.knownNull = null;
<a name="line403"></a>  return obj;
<a name="line404"></a>}
<a name="line405"></a>
<a name="line406"></a>function testGetValueByKeys() {
<a name="line407"></a>  var obj = createTestDeepObject();
<a name="line408"></a>  assertEquals(obj, goog.object.getValueByKeys(obj));
<a name="line409"></a>  assertEquals(obj.a, goog.object.getValueByKeys(obj, &#39;a&#39;));
<a name="line410"></a>  assertEquals(obj.a.b, goog.object.getValueByKeys(obj, &#39;a&#39;, &#39;b&#39;));
<a name="line411"></a>  assertEquals(obj.a.b.c, goog.object.getValueByKeys(obj, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;));
<a name="line412"></a>  assertEquals(obj.a.b.c.d,
<a name="line413"></a>               goog.object.getValueByKeys(obj, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;));
<a name="line414"></a>  assertEquals(8, goog.object.getValueByKeys(obj, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;fooArr&#39;, 3));
<a name="line415"></a>  assertNull(goog.object.getValueByKeys(obj, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;knownNull&#39;));
<a name="line416"></a>  assertUndefined(goog.object.getValueByKeys(obj, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;));
<a name="line417"></a>}
<a name="line418"></a>
<a name="line419"></a>function testGetValueByKeysArraySyntax() {
<a name="line420"></a>  var obj = createTestDeepObject();
<a name="line421"></a>  assertEquals(obj, goog.object.getValueByKeys(obj, []));
<a name="line422"></a>  assertEquals(obj.a, goog.object.getValueByKeys(obj, [&#39;a&#39;]));
<a name="line423"></a>
<a name="line424"></a>  assertEquals(obj.a.b, goog.object.getValueByKeys(obj, [&#39;a&#39;, &#39;b&#39;]));
<a name="line425"></a>  assertEquals(obj.a.b.c, goog.object.getValueByKeys(obj, [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]));
<a name="line426"></a>  assertEquals(obj.a.b.c.d,
<a name="line427"></a>      goog.object.getValueByKeys(obj, [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]));
<a name="line428"></a>  assertEquals(8,
<a name="line429"></a>      goog.object.getValueByKeys(obj, [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;fooArr&#39;, 3]));
<a name="line430"></a>  assertNull(goog.object.getValueByKeys(obj, [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;knownNull&#39;]));
<a name="line431"></a>  assertUndefined(goog.object.getValueByKeys(obj, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;));
<a name="line432"></a>}
<a name="line433"></a>
<a name="line434"></a>function testImmutableView() {
<a name="line435"></a>  if (!Object.isFrozen) return;
<a name="line436"></a>  var x = {propA: 3};
<a name="line437"></a>  var y = goog.object.createImmutableView(x);
<a name="line438"></a>  x.propA = 4;
<a name="line439"></a>  x.propB = 6;
<a name="line440"></a>  y.propA = 5;
<a name="line441"></a>  y.propB = 7;
<a name="line442"></a>  assertEquals(4, x.propA);
<a name="line443"></a>  assertEquals(6, x.propB);
<a name="line444"></a>  assertFalse(goog.object.isImmutableView(x));
<a name="line445"></a>
<a name="line446"></a>  assertEquals(4, y.propA);
<a name="line447"></a>  assertEquals(6, y.propB);
<a name="line448"></a>  assertTrue(goog.object.isImmutableView(y));
<a name="line449"></a>
<a name="line450"></a>  assertFalse(&#39;x and y should be different references&#39;, x == y);
<a name="line451"></a>  assertTrue(
<a name="line452"></a>      &#39;createImmutableView should not create a new view of an immutable object&#39;,
<a name="line453"></a>      y == goog.object.createImmutableView(y));
<a name="line454"></a>}
<a name="line455"></a>
<a name="line456"></a>function testImmutableViewStrict() {
<a name="line457"></a>  &#39;use strict&#39;;
<a name="line458"></a>
<a name="line459"></a>  if (!Object.isFrozen) return;
<a name="line460"></a>  var x = {propA: 3};
<a name="line461"></a>  var y = goog.object.createImmutableView(x);
<a name="line462"></a>  assertThrows(function() {
<a name="line463"></a>    y.propA = 4;
<a name="line464"></a>  });
<a name="line465"></a>  assertThrows(function() {
<a name="line466"></a>    y.propB = 4;
<a name="line467"></a>  });
<a name="line468"></a>}
</pre>


</body>
</html>
